package com.smart.lab.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.smart.lab.MyApplication;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;


public class SPHelper {


    private static SPHelper helper;

    private SharedPreferences mSp;

    private SharedPreferences spOther;

    //TODO 上下文问题
    public static SPHelper getPreferences() {
        if (helper == null) {
            helper = new SPHelper(MyApplication.getAppContext());
        }
        return helper;
    }

    public SPHelper(Context context) {
        mSp = context.getSharedPreferences("app_sp", Context.MODE_PRIVATE);

        spOther = context.getSharedPreferences("app_sp_other", Context.MODE_PRIVATE);
    }

    public void saveString(String key, String value) {
        mSp.edit().putString(key, value).commit();
    }

    public String getString(String key) {
        return mSp.getString(key, "");
    }

    public void saveLong(String key, long value) {
        mSp.edit().putLong(key, value).commit();
    }

    public long getLong(String key) {
        return mSp.getLong(key, -1);
    }

    public void saveInt(String key, int value) {
        mSp.edit().putInt(key, value).commit();
    }

    /**
     * 保存对象对SP
     *
     * @param key
     * @param objectList
     */
    public void saveObject(String key, List<Object> objectList) {
        Gson gson = new Gson();
        String json = gson.toJson(objectList);
        mSp.edit().putString(key, json).commit();
    }

    /**
     * 获取对象
     *
     * @param key
     * @return
     */
    public List<Object> getObject(String key) {
        Gson gson = new Gson();
        String json = mSp.getString(key, null);
        Type type = new TypeToken<List<Object>>() {
        }.getType();
        List<Object> arrayList = gson.fromJson(json, type);
        return arrayList;
    }

    /**
     * 保存List
     *
     * @param tag
     * @param datalist
     */
    public <T> void setDataList(String tag, List<T> datalist) {
        if (null == datalist || datalist.size() <= 0) {
            SharedPreferences.Editor editor = mSp.edit();
            editor.putString(tag, "");
            editor.commit();
            return;
        }

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);

        SharedPreferences.Editor editor = mSp.edit();
        editor.putString(tag, strJson);
        editor.commit();

    }

    /**
     * 获取List
     *
     * @param
     * @return
     */

    public static <T> List<T> getDataList(String json, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        if (TextUtils.isEmpty(json)) {
            return new ArrayList<>();
        }
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            list.add(new Gson().fromJson(elem, cls));
        }
        return list;
    }


    public int getInt(String key) {
        return mSp.getInt(key, 0);
    }

    public void saveBoolean(String key, boolean value) {
        mSp.edit().putBoolean(key, value).commit();
    }

    public boolean getBoolean(String key) {
        return mSp.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return mSp.getBoolean(key, defValue);
    }

    public void saveHashSet(String key, HashSet<String> hashSet) {
        mSp.edit().putStringSet(key, hashSet).commit();
        HashSet<String> ADA = getHashSet(key);
        int i = 0;
    }

    public HashSet<String> getHashSet(String key) {
        HashSet<String> ADA = (HashSet<String>) mSp.getStringSet(key, null);
        return ADA;
    }


    public void cleanr() {
        mSp.edit().clear().apply();
    }


    public void savePrivacy(String key, boolean value) {
        spOther.edit().putBoolean(key, value).commit();
    }

    public boolean getprivacy(String key) {
        return spOther.getBoolean(key, false);
    }

    public void cleanrPrivacy() {
        spOther.edit().clear().apply();
    }


}
